<template>
  <div class="page-filter">
    <div class="page-filter-left">
      <a-button type="primary" @click="addClassify">新建分类</a-button>
      <a-button @click="multipleEdit" v-if="!statusState.isEdit">批量编辑</a-button>
      <a-button type="info" v-else @click="complete">完成</a-button>
      <a-button danger @click="deleteClass">删除</a-button>
    </div>
    <div class="page-filter-right">
      <a-select
        :maxlength="19"
        v-model:value="atlasState.categ_id"
        placeholder="所属分类"
        allowClear
        class="select-m-r-24"
        :getPopupContainer="(triggerNode: any) => triggerNode.parentNode"
      >
        <a-select-option :value="undefined" v-if="classFilterList.length > 0">全部分类</a-select-option>
        <a-select-option :value="item.id" v-for="item in classFilterList" :key="item.id">{{
          item.name
        }}</a-select-option>
      </a-select>
      <a-input
        v-model:value.trim="atlasState.keyword"
        class="input-m-r-16 w-[250px]"
        placeholder="请输入分类/子分类名称关键词"
      />
      <a-button type="primary" class="m-r-8" @click="handleSearch">搜索</a-button>
      <a-button class="btn-m-r-24" @click="handleReset">重置</a-button>
    </div>
  </div>
  <!-- 删除对话框 -->
  <modal-view
    title="提示"
    v-model:visible="deleteVisible"
    okText="确定"
    cancelText="取消"
    :width="458"
    @confirm="deleteConfirm"
    @cancel="deleteCancel"
  >
    <span>确定要删除该分类吗</span>
  </modal-view>
</template>
<script lang="ts" setup>
import { atlasState, statusState, resetState, type ICopyList } from '../state'
import ModalView from '@/components/components/modal/ModalView.vue'
import { message } from 'ant-design-vue'
import { onMounted, ref, toRefs } from 'vue'
import apis from '@/apis'
import { storage } from '@/utils/storage'
const {
  site: { atlas_filter_classify_list, atlas_del_classify, atlas_batchUpdate }
} = apis

const deleteVisible = ref(false)
const siteId = storage.get('siteId')
const classFilterList = ref<ICopyList[]>([])

const emit = defineEmits(['update-list'])
const deleteClass = () => {
  if (!statusState.selectedRowKeys.length) return message.warning('请选择至少一个分类进行删除')
  console.log('statusState.selectedRowKeys: ', statusState.selectedRowKeys)
  deleteVisible.value = true
  statusState.isEdit = false
}
/**
  删除分类对话框事件
 */
const deleteConfirm = (value: any) => {
  console.log('value: ', value)
  atlas_del_classify({ categ_id: statusState.selectedRowKeys }).then(() => {
    emit('update-list')
    deleteVisible.value = false
    message.success('批量删除成功')
  })
}
const deleteCancel = () => {
  deleteVisible.value = false
  statusState.selectedRowKeys = []
}
//获取筛选分类接口
const getCopyAtlasList = () => {
  console.log('getCopyAtlasList: ', 'getCopyAtlasList')
  atlas_filter_classify_list({ site_id: siteId }).then((res: any) => {
    console.log('res11: ', res)
    classFilterList.value = res?.categ ?? []
    console.log('classFilterList.value: ', classFilterList.value)
  })
}
getCopyAtlasList()
//新建分类
const addClassify = () => {
  statusState.addClassify = true
  statusState.isAddClass = true
  statusState.isEdit = false
}
//批量编辑
const multipleEdit = () => {
  if (statusState.atlasList.length <= 0) return message.warning('请添加至少一个分类')
  statusState.isEdit = true
}
//完成
const complete = () => {
  const list: any = statusState.atlasList
  for (var i = 0; i <= list.length; i++) {
    if (list.some((item: any) => item.name === '')) {
      message.warning('分类不能为空')
      return false
    } else if (list[i]?.subcateg?.length && list[i].subcateg.some((item: any) => item.name === '')) {
      message.warning('分类不能为空')
      return false
    }
  }
  statusState.isEdit = false
  console.log(statusState.atlasList, 'atlasList')
  atlas_batchUpdate({ categs: statusState.atlasList }).then(() => {
    message.success('批量编辑成功')
    statusState.isEdit = false
    statusState.selectedRowKeys = []
    emit('update-list')
  })
}
const handleSearch = () => {
  console.log(atlasState)
  emit('update-list')
}
const handleReset = () => {
  resetState()
  console.log(atlasState)
  emit('update-list')
}
onMounted(() => {
  statusState.selectedRowKeys = []
  resetState()
})
</script>

<style lang="less" scoped>
@common-color: #0083ff; // sea blue

.page-filter {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;

  &-left {
    display: flex;
    flex-wrap: nowrap;

    margin-top: 10px;

    .ant-btn:not(:last-child) {
      margin-right: 16px;
    }

    .ant-btn:last-child {
      background-color: rgba(254, 76, 77, 0.08);
      margin-right: 16px;
    }
  }

  &-right {
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .select-m-r-24 {
      width: 196px;
      height: 32px;
      margin: 10px 24px 0 0;
    }

    .input-m-r-16 {
      height: 32px;
      margin: 10px 16px 0 0;
    }

    .btn-m-r-24 {
      margin: 10px 24px 0 0;
    }

    .m-r-8 {
      margin: 10px 8px 0 0;
    }
  }
}
</style>
